

//
//  NetworkTool.swift
//  Soap协议XML解析
//
//  Created by xuNick on 16/7/17.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit
import AFNetworking

class NetworkTool: AFHTTPSessionManager {
    
    enum RequestMethod: String {
        case Post = "post"
        case Get = "get"
    }
    //MARK: - 全局访问点
    static let sharedTools: NetworkTool = {
        
        let tools = NetworkTool()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/xml")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
//        tools.responseSerializer = AFHTTPResponseSerializer()
        tools.responseSerializer = AFJSONResponseSerializer()
        tools.requestSerializer.timeoutInterval = 5.0
        let manager = AFNetworkReachabilityManager.shared()
        
        manager.setReachabilityStatusChange({ (Status) in
            switch Status{
            case .unknown:
                print("未知网络=================")
                break
            case .notReachable:
                print("无连接=================")
                break
            case .reachableViaWWAN:
                print("蜂窝网络=================")
                break
            case .reachableViaWiFi:
                print("WIFI=================")
                break
            }
        })
        return tools
    }()
    
    //MARK: - public
    static func request(method: RequestMethod = .Post,url: String,param: [String: String],success:@escaping (_ response: Any) -> (),failed:@escaping ()->(),netError:(() -> ())? = nil) {
        
        let url = "http://127.0.0.1:8181/v1/json1"
        NetworkTool.sharedTools.get(url, parameters: param, progress: nil, success: { (task, response) in
            
            success(response!)
            print(response!)
        }, failure: { (task, error) in
            
            print(error)
        })
        
    }


}



